package CCN::modules::core_bootstrap;
# Copyright (C) 2006, 2007  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
use strict; use warnings;

use base qw(CCN::Core::Module);

sub _description
{
    return {
        name        => 'core_bootstrap',
        title       => 'Bootstrap',
        description => 'This module provides Bootstrapping for CarrierClass.net.',
        group       => 'Core',
        version     => '0.1',
        requires    => [],
        operations  =>
            {
                install =>
                {
                    do => sub
                    {
                        Register the bootstrap hooks
                    }
                },
                bootstrap =>
                {
                    do => sub
                    {
                        
                        # Part 2: Create the bootstrap (SQLite) database using Jifty.

                        use CCN::Text;

                        $ENV{JIFTY_CONFIG} = CCN::Core::Configuration::get('usr_lib_ccn_XXX').  Provisioning::Local::_directory.'/config.yml';

                        # Create a configuration file for "jifty schema --setup"
                        use CCN::sql_copy::Jifty;
                        use CCNCore::Provision::Database::CCNCore;

                        CCN::sql_copy::Jifty::initialize_sqlite3(
                            $CCNCore::Provisioning::Local::_name,
                            CCNCore::Provision::Database::CCNCore::jifty_env(undef)
                        );

                        # Part 3: Add our local SSH public key to the list of authorized keys.

                        # This is normally done in remote-node-bootstrap, but we are running locally, so do it here.
                        CCN::Scripting::exec_command q(/bin/cat "$HOME/.ssh/id_rsa.pub" >> "$HOME/.ssh/authorized_keys2");
                        return 1;

                    }
                }
            },
    }
}

1;
